package com.wap.framework.dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.sql.Types;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

import com.wap.framework.model.Image;
import com.wap.framework.pagination.PaginationHelper;

public class ImageDAOImpl implements ImageDAO {
	private JdbcTemplate jdbcTemplate;

	private static String countSql = "select count(*) from dbo.tb_image where i_delflag=0";

	private static String listCurrentPage = "select * from (select *,row_number() over(order by i_imageId) as rownum from dbo.tb_image where i_delflag=0) as t where t.rownum>? and t.rownum<=?";

	private static String listImages = "select * from dbo.tb_image where i_delflag=0";

	private static String saveImages = "insert into dbo.tb_image(v_imageName,v_imagePath,v_remark,t_createTime,t_updateTime,v_displayName) values(?,?,?,?,?,?);";

	private static String deleteImages = "update dbo.tb_image set i_delflag=1 where i_imageId=?";

	private static String editImage = "select * from dbo.tb_image where i_imageId=?";

	private static String updateImage = "update dbo.tb_image set v_displayName=?,i_status=?,v_remark=?,t_updateTime=getdate() where i_imageId=?";

	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}

	@SuppressWarnings("unchecked")
	public List<Object> listImages(int pageNum) {
		PaginationHelper ph = new PaginationHelper(pageNum);
		ph.setJdbcTemplate(jdbcTemplate);
		ph.init(countSql);

		Object[] params = new Object[] { ph.getStartIndex(), ph.getEndIndex() };
		int[] types = new int[] { Types.INTEGER, Types.INTEGER };
		List<Image> images = jdbcTemplate.query(listCurrentPage, params, types,
				new ImageRowMapper());

		List result = new ArrayList(2);
		result.add(images);
		result.add(ph);

		return result;
	}

	public void saveImages(final Image[] imageFiles) {
		BatchPreparedStatementSetter bpss = new BatchPreparedStatementSetter() {
			public int getBatchSize() {
				return imageFiles.length;
			}

			public void setValues(PreparedStatement pstmt, int i)
					throws SQLException {
				pstmt.setString(1, imageFiles[i].getImageName());
				pstmt.setString(2, imageFiles[i].getImagePath());
				pstmt.setString(3, imageFiles[i].getRemark());
				pstmt.setTimestamp(4, new Timestamp(new Date().getTime()));
				pstmt.setTimestamp(5, new Timestamp(new Date().getTime()));
				pstmt.setString(6, imageFiles[i].getDisplayName());
			}
		};
		this.jdbcTemplate.batchUpdate(saveImages, bpss);
	}

	public void deleteImages(final Integer[] ids) {
		BatchPreparedStatementSetter bpss = new BatchPreparedStatementSetter() {
			public int getBatchSize() {
				return ids.length;
			}

			public void setValues(PreparedStatement pstmt, int i)
					throws SQLException {
				pstmt.setInt(1, ids[i]);
			}
		};
		this.jdbcTemplate.batchUpdate(deleteImages, bpss);
	}

	@SuppressWarnings("unchecked")
	public Image editImage(int imageId) {
		Object[] params = new Object[] { imageId };
		int[] types = new int[] { Types.INTEGER };
		List<Image> images = jdbcTemplate.query(editImage, params, types,
				new ImageRowMapper());
		if (images == null || images.size() == 0) {
			return null;
		}
		return (Image) images.get(0);
	}

	public void updateImage(Image image) {
		Object[] params = new Object[] { image.getImageName(),
				image.getStatus(), image.getRemark(), image.getId() };
		int[] types = new int[] { Types.VARCHAR, Types.INTEGER, Types.VARCHAR,
				Types.INTEGER };

		this.jdbcTemplate.update(updateImage, params, types);
	}

	class ImageRowMapper implements RowMapper {
		public Object mapRow(ResultSet rs, int index) throws SQLException {
			Image image = new Image();
			image.setId(rs.getInt("i_imageId"));
			image.setImageName(rs.getString("v_imageName"));
			image.setImagePath(rs.getString("v_imagePath"));
			image.setStatus(rs.getInt("i_status"));
			image.setDelFlag(rs.getInt("i_delflag"));
			image.setCreateTime(rs.getTimestamp("t_createTime"));
			image.setUpdateTime(rs.getTimestamp("t_updateTime"));
			image.setRemark(rs.getString("v_remark"));
			image.setDisplayName(rs.getString("v_displayName"));
			return image;
		}
	}
}
